package com.woniu.bh.backstage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.bh.backstage.dao.CouponMapper;
import com.woniu.bh.backstage.dto.CouponDto;
import com.woniu.bh.backstage.param.QueryCouponParam;
import com.woniu.bh.backstage.service.ICouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.common.core.domain.coupon.Coupon;
import com.woniu.bh.common.core.dto.ResultDto;
import com.woniu.bh.common.core.exception.ProjectExceptionResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.PortUnreachableException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cxb
 * @since 2020-10-13
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {

    @Autowired
    private CouponMapper couponMapper;
    @Override
    public ResultDto selectPage(QueryCouponParam queryCouponParam) {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotEmpty(queryCouponParam.getCouponName())){
            wrapper.eq("coupon_name",queryCouponParam.getCouponName());
        }
        if(null!=queryCouponParam.getCouponType()){
            wrapper.eq("coupon_type",queryCouponParam.getCouponType());
        }
        if(null!=queryCouponParam.getCouponFacevalueMax()){
            wrapper.lt("coupon_facevalue",queryCouponParam.getCouponFacevalueMax());
        }
        if(null!=queryCouponParam.getCouponFacevalueMin()){
            wrapper.gt("coupon_facevalue",queryCouponParam.getCouponFacevalueMin());
        }
        if(null!=queryCouponParam.getCouponValidTimeMax()){
            wrapper.lt("coupon_valid_time",queryCouponParam.getCouponValidTimeMax());
        }
        if(null!=queryCouponParam.getCouponValidTimeMix()){
            wrapper.gt("coupon_valid_time",queryCouponParam.getCouponValidTimeMix());
        }
        IPage iPage = couponMapper.selectPage(new Page(queryCouponParam.getPageIndex(),queryCouponParam.getPageSize()),wrapper);
        if(null!=iPage.getRecords()){
            return ResultDto.success(iPage);
        }
        return ResultDto.fail(ProjectExceptionResult.DATA_NOT_FOUND);
    }
}
